﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using CsvHelper;

namespace AIC_Notification
{
    public static class SchoolDB
    {
        static private IList<School> Schools = null;

        // UC Schools
        public enum UC { B, D, I, LA, M, R, SB, SC, SD }
        static public string[] UC_ShortName = { "UCB", "UCD", "UCI", "UCLA", "UCM", "UCR", "UCSB", "UCSC", "UCSD" };
        static public School[] UC_Schools = null;

        static public bool Initialize()
        {

            // Load admission statistics information from CSV from Salesforce
            IList<SchoolAdmissionStatistics> SchoolStatistics = new List<SchoolAdmissionStatistics>();
            try
            {
                using (StreamReader sr = new StreamReader(Global.InputDirectory + "Statistics.csv"))
                {
                    //Csv reader reads the stream
                    CsvReader csvread = new CsvReader(sr);
                    csvread.Configuration.RegisterClassMap<AdmissionResultMap>();
                    csvread.Configuration.HeaderValidated = null;

                    //csvread will fetch all record in one go to the IEnumerable object record
                    SchoolStatistics = csvread.GetRecords<SchoolAdmissionStatistics>().ToList();

                    sr.Close();
                }
            }
            catch
            {
                Global.SysUtil.Log(String.Format("Failed to read from Statistics file: {0}", "Statistics.csv"), 2);
                return false;
            }

            // Load schools information from CSV from Salesforce
            try
            {
                using (StreamReader sr = new StreamReader(Global.InputDirectory + Global.config.SchoolFileName))
                {
                    //Csv reader reads the stream
                    CsvReader csvread = new CsvReader(sr);
                    csvread.Configuration.RegisterClassMap<SchoolMap>();
                    csvread.Configuration.HeaderValidated = null;

                    //csvread will fetch all record in one go to the IEnumerable object record
                    Schools = csvread.GetRecords<School>().ToList();

                    sr.Close();
                }
            }
            catch
            {
                Global.SysUtil.Log(String.Format("Failed to read from school file: {0}", Global.config.SchoolFileName), 2);
                return false;
            }

            // Connect school info with statistics
            foreach (var School in Schools)
            {
                School.AdmissionResultObj = SchoolStatistics.SingleOrDefault(AdmissionResult => AdmissionResult.SchoolName == School.Name);

                if (School.Ranking != "")
                    School.RankingInt = Int32.Parse(School.Ranking);
                else
                    School.RankingInt = 0;
            }

            // Setup UC Schools
            UC_Schools = new School[Enum.GetNames(typeof(SchoolDB.UC)).Length];
            UC_Schools[(int)UC.B] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.B], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.D] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.D], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.I] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.I], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.LA] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.LA], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.M] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.M], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.R] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.R], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.SB] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.SB], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.SC] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.SC], StringComparison.CurrentCultureIgnoreCase));
            UC_Schools[(int)UC.SD] = Schools.SingleOrDefault(School => School.ShortName.Equals(UC_ShortName[(int)UC.SD], StringComparison.CurrentCultureIgnoreCase));

            return true;
        }

        static public School GetSchoolbyShortName(string ShortName)
        {
            return Schools.SingleOrDefault(School => School.ShortName.Equals(ShortName, StringComparison.CurrentCultureIgnoreCase));
        }

        static public School GetSchoolbyID(string SchoolID)
        {
            return Schools.SingleOrDefault(School => School.ID.Equals(SchoolID));
        }

        static public bool IsUCSchool(string SchoolID)
        {
            foreach (var UCSchool in UC_Schools)
            {
                if (SchoolID == UCSchool.ID)
                    return true;
            }

            return false;
        }

        static public School GetUCSchool(string ShortName)
        {
            int TotalUCSchool = Enum.GetNames(typeof(SchoolDB.UC)).Length;
            School UC_School = null;

            for (int i = 0; i < TotalUCSchool; i++)
            {
                if (ShortName.Equals(UC_ShortName[i], StringComparison.CurrentCultureIgnoreCase))
                {
                    UC_School = UC_Schools[i];
                    break;
                }
            }

            return UC_School;
        }

        static public School GetUCSchool(SchoolDB.UC UC_School)
        {
            return UC_Schools[(int)UC_School];
        }
    }
}
